package da;

import data.ArmyBuildingType;
import data.BuildingType;
import data.ProductionBuildingType;
import data.TownCenterBuildingType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Stijn Verslycken, Jonas Van Den Bossche
 * @date 2007-2008
 */
public class BuildingTypeDA {

    private ResourceBundle bundle;
    private static BuildingTypeDA instance = null;
    private final int TOWNCENTER = 0;
    private final int PRODUCTION = 1;
    private final int ARMY = 2;

    private BuildingTypeDA() {
        bundle = ResourceBundle.getBundle("da.dbconsts");
    }

    public static BuildingTypeDA getInstance() {
        if (instance == null) {
            instance = new BuildingTypeDA();
        }
        return instance;
    }

    /**
     * 
     * @return
     */
    public Vector<BuildingType> getAllBuildingTypes() {
        Vector<BuildingType> btypes = null;
        BuildingType bdt = null;

        String sql = bundle.getString("getAllBuildingTypes");

        Connection dbcon = null;
        try {
            dbcon = DatabaseConnection.getInstance().getConnection();
            PreparedStatement prepstat = dbcon.prepareStatement(sql);
            try {

                ResultSet rs = prepstat.executeQuery();

                btypes = new Vector<BuildingType>();
                while (rs.next()) {
                    int type = rs.getInt("type");

                    if (type == TOWNCENTER) {
                        bdt = new TownCenterBuildingType(rs.getString("name"), rs.getInt("min_level"), rs.getInt("max_level"),
                                rs.getInt("cost_food"), rs.getInt("cost_wood"), rs.getInt("cost_stone"), rs.getInt("cost_iron"),
                                rs.getInt("cost_gold"), rs.getInt("default_strength"), rs.getString("image_url"), rs.getString("btn_image_url"));

                    } else if (type == PRODUCTION) {
                        bdt = new ProductionBuildingType(rs.getString("name"), rs.getInt("min_level"), rs.getInt("max_level"),
                                rs.getInt("cost_food"), rs.getInt("cost_wood"), rs.getInt("cost_stone"), rs.getInt("cost_iron"),
                                rs.getInt("cost_gold"), rs.getInt("default_capacity"), rs.getString("resource_type"), rs.getInt("default_strength"),
                                rs.getString("image_url"), rs.getString("btn_image_url"));
                    } else if (type == ARMY) {
                        bdt = new ArmyBuildingType(rs.getString("name"), rs.getInt("min_level"), rs.getInt("max_level"),
                                rs.getInt("cost_food"), rs.getInt("cost_wood"), rs.getInt("cost_stone"), rs.getInt("cost_iron"),
                                rs.getInt("cost_gold"), rs.getInt("default_strength"), rs.getString("image_url"), rs.getString("btn_image_url"));
                    }


                    // add to list 
                    btypes.add(bdt);
                }

            } finally {
                prepstat.close();
                dbcon.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(BuildingDA.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("getAllBuildingTypes error -->" + ex.toString());
        }

        return btypes;
    }

    /**
     * 
     * @return
     */
    public Vector<ProductionBuildingType> getAllProductionBuildingTypes() {
        Vector<ProductionBuildingType> pbtypes = null;
        ProductionBuildingType pbdt = null;

        String sql = bundle.getString("getBuildingTypesByKind");

        Connection dbcon = null;
        try {
            dbcon = DatabaseConnection.getInstance().getConnection();
            PreparedStatement prepstat = dbcon.prepareStatement(sql);
            try {
                prepstat.setInt(1, PRODUCTION);
                ResultSet rs = prepstat.executeQuery();

                pbtypes = new Vector<ProductionBuildingType>();
                while (rs.next()) {

                    pbdt = new ProductionBuildingType(rs.getString("name"), rs.getInt("min_level"), rs.getInt("max_level"),
                            rs.getInt("cost_food"), rs.getInt("cost_wood"), rs.getInt("cost_stone"), rs.getInt("cost_iron"),
                            rs.getInt("cost_gold"), rs.getInt("default_capacity"), rs.getString("resource_type"), rs.getInt("default_strength"),
                            rs.getString("image_url"), rs.getString("btn_image_url"));



                    // add to list 
                    pbtypes.add(pbdt);
                }

            } finally {
                prepstat.close();
                dbcon.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(BuildingDA.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("getAllProductionBuildingTypes error -->" + ex.toString());
        }

        return pbtypes;
    }

    /**
     * 
     * @return
     */
    public Vector<ArmyBuildingType> getAllArmyBuildingTypes() {
        Vector<ArmyBuildingType> abtypes = null;
        ArmyBuildingType abdt = null;
        
            String sql = bundle.getString("getBuildingTypesByKind");

        Connection dbcon = null;
        try {
            dbcon = DatabaseConnection.getInstance().getConnection();
            PreparedStatement prepstat = dbcon.prepareStatement(sql);
            try {
                prepstat.setInt(1, PRODUCTION);
                ResultSet rs = prepstat.executeQuery();

                abtypes = new Vector<ArmyBuildingType>();
                while (rs.next()) {

                    abdt = new ArmyBuildingType(rs.getString("name"), rs.getInt("min_level"), rs.getInt("max_level"),
                            rs.getInt("cost_food"), rs.getInt("cost_wood"), rs.getInt("cost_stone"), rs.getInt("cost_iron"),
                            rs.getInt("cost_gold"), rs.getInt("default_strength"), rs.getString("image_url"), rs.getString("btn_image_url"));



                    // add to list 
                    abtypes.add(abdt);
                }

            } finally {
                prepstat.close();
                dbcon.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(BuildingDA.class.getName()).log(Level.SEVERE, null, ex);
            System.err.println("getAllArmyBuildingTypes error -->" + ex.toString());
        }
        
        return abtypes;
    }
}
